package com.zmn.plat.admin.controller.part.category.map;

import java.util.ArrayList;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.dto.ResultDTO;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartCategoryMap;

/**
 * 类描述： PartCategoryMapModifyController
 *
 * @author DaiZhichao
 * @since 2018/11/6 20:51
 */
@RestController
@RequestMapping("/part/category/map/ajax")
public class PartCategoryMapModifyController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartCategoryMapModifyController.class);

    /**
     * 描述:[Ajax] 配件分类产品分类映射 添加/修改
     *
     * @author DaiZhichao
     * @since 2018/12/13 17:51
     */
    @RequestMapping(value = "/addEdit")
    @RequiresPermissions("/part/category/map/ajax/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody PartCategoryMap partCategoryMap) {
        String lable = null;

        if (partCategoryMapService.findIdenticalMap(partCategoryMap)) {
            return ResponseDTO.fail("分类关联已存在，请勿重复关联");
        }

        try {
            if (Objects.isNull(partCategoryMap.getMapId())) {
                lable = "新增";
                // 产品-配件 映射关系
                partCategoryMapService.insert(partCategoryMap, getMcStaff());
            } else {
                lable = "修改";
                partCategoryMapService.updateByKey(partCategoryMap, getMcStaff());
            }
        } catch (Exception e) {
            logger.error(String.format("%s失败！", lable), e);
            return ResponseDTO.fail(String.format("%s失败！", lable));
        }

        return ResponseDTO.success(String.format("%s成功！", lable));
    }

    /**
     * 描述:[Ajax] 配件分类产品分类映射 批量删除
     *
     * @author DaiZhichao
     * @since 2018/12/13 17:51
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("/part/category/map/ajax/addEdit.action")
    public ResponseDTO delete(HttpServletRequest request, @RequestParam(value = "ids[]", defaultValue = "") ArrayList<Integer> ids) {

        try {
            partCategoryMapService.deleteByKeyBatch(ids);
        } catch (Exception e) {
            logger.error("批量删除失败", e);
            return ResponseDTO.fail("批量删除失败");
        }
        return ResponseDTO.success(true,"删除成功");
    }

}
